//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include "aura.h"
#include "wince.h"
#include "private.h"

AURA_API AuraProcess_GetCurrentId(
        /* [out] */ Aura_ProcessId * pProcessId)
{
    assert(NULL != pProcessId);
    *pProcessId = GetCurrentProcessId();

    return AURA_NOERROR;
}

AURA_API_(void) AuraProcess_Exit(
        /* [in] */ Aura_ExitCode exitCode)
{
    ExitProcess((UINT)exitCode);

    assert(0);
    return ;
}

AURA_API_(Aura_PWChar) AuraProcess_GetCommandLine()
{
    static wchar_t g_tempCmd[128];
    wchar_t * cmd = GetCommandLineW();

    wcscpy(g_tempCmd, L"myTempName.exe  ");
    wcscat(g_tempCmd, cmd);

    return (Aura_PWChar)g_tempCmd;
}

AURA_API AuraProcess_Create(
        /* [in] */ Aura_PWChar imageName,
        /* [in] */ Aura_PWChar arguments,
        /* [out] */ Aura_Handle * phProcess,
        /* [out] */ Aura_ProcessId * pProcessId)
{
    PROCESS_INFORMATION processInfo;
    ProcessObject *pProcObject = NULL;
    Aura_Bool bSuccess = FALSE;

    assert(AURA_NULL != imageName);
    assert(L'\0' != (wchar_t)imageName[0]);

    pProcObject = (ProcessObject*)Aura_malloc(sizeof(ProcessObject));
    if (NULL == pProcObject) {
        return AURA_E_OUT_OF_MEMORY;
    }
    memset(pProcObject, 0, sizeof(ProcessObject));
    memset((void *)&processInfo, 0, sizeof(PROCESS_INFORMATION));

    bSuccess = CreateProcessW((LPCWSTR )imageName,
                   (NULL == arguments) ? NULL : (LPCWSTR)arguments,
                   NULL, NULL,
                   FALSE,
                   0,
                   NULL, NULL, NULL,
                   &processInfo);
    if (!bSuccess) {
        Aura_free((void *)pProcObject);
        return GetLastECodeError();
    }

    CloseHandle(processInfo.hThread);
    if (NULL != phProcess) {
        pProcObject->m_processId = processInfo.dwProcessId;
        pProcObject->m_hProcess  = processInfo.hProcess;
        pProcObject->m_index     = DETACHED_INDEX;

        *phProcess = (Aura_Handle)pProcObject;
    }
    else {
        CloseHandle(processInfo.hProcess);
        Aura_free((void*)pProcObject);
        pProcObject = NULL;
    }
    if (NULL != pProcessId) {
        *pProcessId = (Aura_ProcessId)processInfo.dwProcessId;
    }

    return AURA_NOERROR;
}

AURA_API AuraProcess_Open(
        /* [in] */ Aura_ProcessId processId,
        /* [out] */ Aura_Handle * phProcess)
{
    return AURA_NOERROR;
}

AURA_API AuraProcess_Kill(
        /* [in] */ Aura_Handle hProcess,
        /* [in] */ Aura_ExitCode exitCode)
{
    ProcessObject * pProcObject = (ProcessObject *)hProcess;
    BOOL bSuccess = FALSE;

    assert(NULL != hProcess);

    bSuccess = TerminateProcess(pProcObject->m_hProcess, (INT)exitCode);
    if (!bSuccess) {
        goto E_FAIL_EXIT;
    }
    return AURA_NOERROR;

E_FAIL_EXIT:
    return GetLastECodeError();
}

AURA_API AuraProcess_Destroy(
        /* [in] */ Aura_Handle hProcess)
{
    ProcessObject * pProcObject = (ProcessObject *)hProcess;
    BOOL bSuccess = FALSE;
    Aura_ECode ec = AURA_NOERROR;

    assert(NULL != hProcess);

    bSuccess = CloseHandle(pProcObject->m_hProcess);
    if (!bSuccess) {
        ec = GetLastECodeError();
    }
    Aura_free(pProcObject);

    return ec;
}

AURA_API AuraProcess_GetPriorityRank(
        /* [in] */ Aura_Handle hProcess,
        /* [out] */ Aura_ThreadPriorityRank * pPriorityRank)
{
    return AURA_NOERROR;
}

AURA_API AuraProcess_SetPriorityRank(
        /* [in] */ Aura_Handle hProcess,
        /* [out] */ Aura_ThreadPriorityRank priorityRank)
{
    return AURA_NOERROR;
}

AURA_API AuraProcess_GetLifespan(
        /* [in] */ Aura_Handle hProcess,
        /* [out] */ Aura_SystemTime * pStartTime,
        /* [out] */ Aura_SystemTime * pExitTime)
{
    return AURA_NOERROR;
}

AURA_API AuraProcess_GetProcessorTimes(
        /* [in] */ Aura_Handle hProcess,
        /* [out] */ Aura_Millisecond32 * pUserTime,
        /* [out] */ Aura_Millisecond32 * pPrivilegedTime)
{
    return AURA_NOERROR;
}

AURA_API AuraProcess_GetExitCode(
        /* [in] */ Aura_Handle hProcess,
        /* [out] */ Aura_ExitCode * pExitCode)
{
    return AURA_NOERROR;
}

AURA_API AuraProcess_GetMemoryInformation(
        /* [in] */ Aura_Handle hProcess,
        /* [out] */ Aura_Int32 * pPrivateVirtualMemorySize,
        /* [out] */ Aura_Int32 * pSharedVirtualMemorySize,
        /* [out] */ Aura_Int32 * pPrivatePhysicalMemorySize,
        /* [out] */ Aura_Int32 * pSharedPhysicalMemorySize)
{
    return AURA_NOERROR;
}

AURA_API AuraCentralContext_Set(
    /* [in] */ PDoAtExceptionalExitOfProcess pDoAtExtFunc)
{
    return AURA_NOERROR;
}

AURA_API_(void) AuraCentralContext_Clear()
{
    return ;
}

